package com.wibo.account;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmotionsParser {
	private Context mContext;
	//private String[] mSmileyTexts;
	private Pattern mPattern;
	private HashMap<String, Integer> mSmileyToRes;

	public EmotionsParser(Context context) {
		mContext = context;
		mSmileyToRes = buildSmileyToRes();
		mPattern = buildPattern();
	}


	private HashMap<String, Integer> buildSmileyToRes() {
		if (SendWeiboActivity.Emotions.length != SendWeiboActivity.EmotionStrings.length) {
			// Log.w("SmileyParser", "Smiley resource ID/text mismatch");
			// 表情的数量需要和数组定义的长度一致！
			throw new IllegalStateException("Smiley resource ID/text mismatch");
		}

		HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(
				SendWeiboActivity.EmotionStrings.length);
		for (int i = 0; i < SendWeiboActivity.EmotionStrings.length; i++) {
			smileyToRes.put(SendWeiboActivity.EmotionStrings[i], SendWeiboActivity.Emotions[i]);
		}

		return smileyToRes;
	}

	// 构建正则表达式
	private Pattern buildPattern() {
		StringBuilder patternString = new StringBuilder(SendWeiboActivity.EmotionStrings.length * 3);
		patternString.append('(');
		for (String s : SendWeiboActivity.EmotionStrings) {
			patternString.append(Pattern.quote(s));
			patternString.append('|');
		}
		patternString.replace(patternString.length() - 1, patternString
				.length(), ")");

		return Pattern.compile(patternString.toString());
	}

	// 根据文本替换成图片
	public CharSequence replace(CharSequence text, int width, int height) {
		SpannableStringBuilder builder = new SpannableStringBuilder(text);
		Matcher matcher = mPattern.matcher(text);
		while (matcher.find()) {
			int resId = mSmileyToRes.get(matcher.group());
			//得到要显示图片的资源
			Drawable d = mContext.getResources().getDrawable(resId);
			//设置高度
			d.setBounds(0, 0, width, height/*d.getIntrinsicWidth(), d.getIntrinsicHeight()*/);
			builder.setSpan(new ImageSpan(d, ImageSpan.ALIGN_BOTTOM), matcher.start(),
					matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return builder;
	}
	
	public CharSequence highlight(CharSequence text, int start, int end, int color){   
        SpannableStringBuilder spannable = new SpannableStringBuilder(text);//用于可变字符串   
        ForegroundColorSpan span=new ForegroundColorSpan(color);   
        //CharacterStyle span2 = new UnderlineSpan();
        StyleSpan span3 = new StyleSpan(android.graphics.Typeface.BOLD); //本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接：http://www.linuxidc.com/Linux/2011-08/40987.htm
        
        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
        //spannable.setSpan(span2, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        spannable.setSpan(span3, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
        
        return spannable;   
    } 
}

/*//SpannableString连续的字符串，长度不可变，同时可以附加一些object;可变的话使用SpannableStringBuilder，参考sdk文档
SpannableString ss = new SpannableString(getText().toString()+"[smile]");  
//得到要显示图片的资源
Drawable d = getResources().getDrawable(id);
//设置高度
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//跨度底部应与周围文本的基线对齐
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);  
//附加图片
ss.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
setText(ss);*/
